﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;
using Uinside.CreditSystem.DomainModels.Models;
using Uinside.CreditSystem.Services;
using Uinside.CreditSystem.WPF.Styles;
using Uinside.CreditSystem.WPF.ViewModels;

namespace Uinside.CreditSystem.WPF.Tools
{
    public class GlobalDataManager : ViewModelBase
    {
        //#region Load DataSource In Background thread

        //public static Thread BackgroundLoadDataSourceThread;
        //private static ObservableCollection<IDataUpdatable> DataItems;

        //public static void AddMonitoringDataSource(IDataUpdatable dataSource)
        //{
        //    if (!DataItems.Contains(dataSource))
        //    {
        //        DataItems.Add(dataSource);
        //    }
        //}

        //public static void MonitorDataSource()
        //{
        //    while (true)
        //    {
        //        if (DataItems.Count <= 0) continue;
        //        DataItems[0].LoadData();
        //        DataItems.RemoveAt(0);
        //    }
        //}

        //static GlobalDataManager()
        //{
        //    BackgroundLoadDataSourceThread = new Thread(MonitorDataSource) { IsBackground = true };
        //    DataItems = new ObservableCollection<IDataUpdatable>();
        //    DataItems.CollectionChanged += DataItems_CollectionChanged;
        //    BackgroundLoadDataSourceThread.Start();
        //}

        //static void DataItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        //{
        //    if (DataItems.Count == 0)
        //    {
        //        BackgroundLoadDataSourceThread.Suspend();
        //    }
        //    else if (BackgroundLoadDataSourceThread.ThreadState.HasFlag(ThreadState.Suspended))
        //    {
        //        BackgroundLoadDataSourceThread.Resume();
        //    }
        //}

        //#endregion
        private static GlobalDataManager instance;
        public static GlobalDataManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new GlobalDataManager(new CreditContext());
                }
                return instance;
            }
        }

        private CreditContext _creditContext;
        private BanksService _bankService;
        private BankAccountStatusService _bankAccountStatusService;
        private PaymentStatusService _paymentStatusService;
        private PaymentTypeService _paymentTypeService;
        private CustomerService _customerService;
        private OrderStatusService _orderStatusService;
        private GlobalDataManager(CreditContext creditContext)
        {
            _creditContext = creditContext;
            _bankService = new BanksService(creditContext);
            _bankAccountStatusService = new BankAccountStatusService(creditContext);
            _paymentStatusService = new PaymentStatusService(creditContext);
            _paymentTypeService = new PaymentTypeService(creditContext);
            _customerService = new CustomerService(creditContext);
            _customerService = new CustomerService(creditContext);
            _orderStatusService = new OrderStatusService(creditContext);
            RunBackgroundWorker((s, e) =>
            {
                _banks = new ObservableCollection<Setting>(_bankService.GetAll());
                _bankAccountStatus = new ObservableCollection<Setting>(_bankAccountStatusService.GetAll());
                _paymentType = new ObservableCollection<Setting>(_paymentTypeService.GetAll());
                _paymentStatuses = new ObservableCollection<Setting>(_paymentStatusService.GetAll());
                _customers = new ObservableCollection<Customer>(_customerService.GetAll());
                _orderStatuses = new ObservableCollection<Setting>(_orderStatusService.GetAll());

            }, (s, e) =>
            {
                OnPropertyChanged("Banks");
                OnPropertyChanged("BankAccountStatus");
                OnPropertyChanged("PayamentStatuses");
                OnPropertyChanged("PaymentType");
                OnPropertyChanged("Customers");
                OnPropertyChanged("OrderStatuses");
            });
        }

        private IList<EntryValue> _numbersOfYear;
        public IList<EntryValue> NumbersOfYear
        {
            get
            {
                if (_numbersOfYear == null)
                {
                    _numbersOfYear = new List<EntryValue>();
                    for (int i = 0; i < 6; i++)
                    {
                        _numbersOfYear.Add(new EntryValue() { Value = i, Name = i == 0 ? "年" : i + "年" });
                    }
                }
                return _numbersOfYear;
            }
        }


        private IList<EntryValue> _numbersOfStages;
        public IList<EntryValue> NumberOfStages
        {
            get
            {
                if (_numbersOfStages == null)
                {
                    _numbersOfStages = new List<EntryValue>
                    {
                    new EntryValue(){ Value = 0, Name = "每月"},
                    new EntryValue(){ Value = 1, Name = "每季度"},
                    new EntryValue(){ Value = 2, Name = "每年"},
                    };

                }
                return _numbersOfStages;
            }
        }

        private IList<EntryValue> _numbersOfMonth;
        public IList<EntryValue> NumbersOfMonth
        {
            get
            {
                if (_numbersOfMonth == null)
                {
                    _numbersOfMonth = new List<EntryValue>();
                    for (int i = 0; i < 12; i++)
                    {
                        _numbersOfMonth.Add(new EntryValue() { Value = i, Name = i == 0 ? "月" : i + "月" });
                    }
                }
                return _numbersOfMonth;
            }

        }

        private IList<EntryValue> _numbersOfDay;
        public IList<EntryValue> NumbersOfDay
        {
            get
            {
                if (_numbersOfDay == null)
                {
                    _numbersOfDay = new List<EntryValue>();
                    for (int i = 0; i < 30; i++)
                    {
                        _numbersOfDay.Add(new EntryValue { Value = i, Name = i == 0 ? "日" : i + "日" });
                    }
                }
                return _numbersOfDay;
            }
        }


        private ObservableCollection<Customer> _customers;
        public ObservableCollection<Customer> Customers
        {
            get
            {
                return _customers;
            }
        }

        private ObservableCollection<Setting> _orderStatuses;
        public ObservableCollection<Setting> OrderStatuses
        {
            get
            {
                return _orderStatuses;
            }
        }

        private ObservableCollection<Setting> _paymentStatuses;
        public ObservableCollection<Setting> PayamentStatuses
        {
            get
            {
                return _paymentStatuses;
            }
        }

        private ObservableCollection<Setting> _paymentType;
        public ObservableCollection<Setting> PaymentType
        {
            get
            {
                return _paymentType;
            }
        }

        private ObservableCollection<Setting> _banks;
        public ObservableCollection<Setting> Banks
        {
            get
            {
                //if (_banks == null)
                //{

                //    //RunBackgroundWorker((s, e) =>
                //    //{
                //    _banks = new ObservableCollection<Setting>(_bankService.GetAll());

                //    //}, (s, e) =>
                //    //{
                //    //    OnPropertyChanged("Banks");
                //    //});
                //}
                return _banks;
            }
        }


        private ObservableCollection<Setting> _bankAccountStatus;
        public ObservableCollection<Setting> BankAccountStatus
        {
            get
            {
                //if (_bankAccountStatus == null)
                //{

                //    //    RunBackgroundWorker((s, e) =>
                //    //    {
                //    _bankAccountStatus = new ObservableCollection<Setting>(_bankAccountStatusService.GetAll());

                //    //}, (s, e) =>
                //    //{
                //    //    OnPropertyChanged("BankAccountStatus");
                //    //});
                //}
                return _bankAccountStatus;
            }
        }
    }
}
